import 'package:flutter/material.dart';
import 'package:toastification/toastification.dart';

/// 自定义Toast提示工具类
/// 提供三种风格：成功、错误和警告
class ToastUtils {
  /// 显示成功提示
  static void showSuccess(BuildContext context, {String message = '操作已完成', Duration? duration}) {
    _showCustomToast(
      context: context, 
      message: message,
      iconData: Icons.done,
      iconBackgroundColor: Colors.greenAccent,
      duration: duration ?? const Duration(seconds: 3),
    );
  }

  /// 显示错误提示
  static void showError(BuildContext context, {String message = '操作失败', Duration? duration}) {
    _showCustomToast(
      context: context, 
      message: message,
      iconData: Icons.close,
      iconBackgroundColor: Colors.redAccent,
      duration: duration ?? const Duration(seconds: 3),
    );
  }
  static void showInfo(BuildContext context, {String message = '操作提示', Duration? duration}) {
    _showCustomToast(
      context: context, 
      message: message,
      iconData: Icons.info,
      iconBackgroundColor: Colors.blueAccent,
      duration: duration ?? const Duration(seconds: 3),
    );
  }

  /// 显示警告提示
  static void showWarning(BuildContext context, {String message = '操作警告', Duration? duration}) {
    _showCustomToast(
      context: context, 
      message: message,
      iconData: Icons.warning,
      iconBackgroundColor: Colors.amberAccent,
      duration: duration ?? const Duration(seconds: 3),
    );
  }

  /// 通用提示框实现
  static void _showCustomToast({
    required BuildContext context,
    required String message,
    required IconData iconData,
    required Color iconBackgroundColor,
    required Duration duration,
  }) {
    toastification.showCustom(
      context: context,
      autoCloseDuration: duration,
      alignment: Alignment.topRight,
      animationBuilder: (context, animation, alignment, child) {
        return FadeTransition(
          opacity: animation,
          child: ScaleTransition(
            scale: Tween<double>(begin: 0.8, end: 1.0).animate(animation),
            child: child,
          ),
        );
      },
      builder: (context, holder) {
        return Align(
          alignment: Alignment.topRight,
          child: LayoutBuilder(
            builder: (context, constraints) {
              // 优化的宽度计算：根据文本长度智能调整
              final TextPainter textPainter = TextPainter(
                text: TextSpan(
                  text: message,
                  style: const TextStyle(
                    fontSize: 14,
                    fontWeight: FontWeight.w500,
                    letterSpacing: 0.5,
                  ),
                ),
                maxLines: 2,
                textDirection: TextDirection.ltr,
              )..layout(maxWidth: 280);
              
              // 精确计算容器宽度：文本宽度 + 图标(24) + 间距(12+8) + 关闭按钮(22) + 内边距(40)
              double containerWidth = textPainter.width + 106;
              // 优化的宽度范围：更紧凑优雅
              containerWidth = containerWidth.clamp(160.0, 320.0);

              return Container(
                width: containerWidth,
                constraints: const BoxConstraints(
                  minHeight: 50,
                  maxHeight: 80,
                ),
                margin: const EdgeInsets.all(16),
                padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 14),
                decoration: BoxDecoration(
                  gradient: LinearGradient(
                    colors: [
                      Colors.black.withOpacity(0.85),
                      Colors.black.withOpacity(0.75),
                    ],
                    begin: Alignment.topLeft,
                    end: Alignment.bottomRight,
                  ),
                  borderRadius: BorderRadius.circular(25),
                  boxShadow: [
                    BoxShadow(
                      color: Colors.black.withOpacity(0.25),
                      blurRadius: 24,
                      spreadRadius: 0,
                      offset: const Offset(0, 8),
                    ),
                    BoxShadow(
                      color: Colors.black.withOpacity(0.08),
                      blurRadius: 6,
                      spreadRadius: 0,
                      offset: const Offset(0, 2),
                    ),
                  ],
                ),
                child: Row(
                  mainAxisSize: MainAxisSize.min,
                  children: [
                    Container(
                      width: 24,
                      height: 24,
                      decoration: BoxDecoration(
                        color: iconBackgroundColor,
                        borderRadius: BorderRadius.circular(12),
                      ),
                      child: Icon(
                        iconData,
                        color: Colors.black,
                        size: 14,
                      ),
                    ),
                    const SizedBox(width: 12),
                    Flexible(
                      child: Text(
                        message,
                        style: const TextStyle(
                          color: Colors.white,
                          fontSize: 14,
                          fontWeight: FontWeight.w500,
                          letterSpacing: 0.5,
                          height: 1.3,
                        ),
                        maxLines: 2,
                        overflow: TextOverflow.ellipsis,
                        textAlign: TextAlign.left,
                      ),
                    ),
                    const SizedBox(width: 8),
                    GestureDetector(
                      onTap: () => toastification.dismissById(holder.id),
                      child: Container(
                        padding: const EdgeInsets.all(4.0),
                        decoration: BoxDecoration(
                          color: Colors.white.withOpacity(0.1),
                          borderRadius: BorderRadius.circular(10),
                        ),
                        child: const Icon(
                          Icons.close,
                          color: Colors.white70,
                          size: 14,
                        ),
                      ),
                    ),
                  ],
                ),
              );
            },
          ),
        );
      },
    );
  }
}
